package com.ff.discounts.component.free

import android.view.Menu
import androidx.compose.material.*
import android.view.MenuItem
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.recyclerview.widget.LinearLayoutManager
import com.ff.discounts.R
import com.ff.discounts.activity.BaseTitleActivity
import com.ff.discounts.component.freedetail.FreeDetailActivity
import com.ff.discounts.component.returnmaney.ReturnMoneyActivity
import com.ff.discounts.component.tab.home.GoodsAdapter
import com.ff.discounts.component.tab.home.HistoryData
import com.ff.discounts.databinding.ActivityFreeBinding
import com.ff.discounts.library.log.HiLog
import com.qmuiteam.qmui.util.QMUIStatusBarHelper


class FreeActivity : BaseTitleActivity<ActivityFreeBinding>() {
    private lateinit var goodsAdapter: GoodsAdapter


    override fun initDatum() {
        super.initDatum()


        // 绑定goodsAdapter
        goodsAdapter = GoodsAdapter()
        binding.goodsList.adapter = goodsAdapter
        goodsAdapter.submitList(historyDataList)


    }
    /**
    * 返回菜单
    *
    * @param menu
    * @return
    */
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.record, menu)
        return true
    }

    /**
     * 菜单点击了回调
     * @param item
     * @return
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home ->
                //Toolbar返回按钮点击
                onBackPressed()
            R.id.action_record ->
                startActivity(ReturnMoneyActivity::class.java)
        }
        return super.onOptionsItemSelected(item)
    }

    override fun initListeners() {
        super.initListeners()

        goodsAdapter.setOnItemClickListener{
                adapter,view,position ->
            startActivity(FreeDetailActivity::class.java)
        }


        //监听列表滚动，目的是显示标题栏
//        binding!!.scrollContent.setOnScrollChangeListener(View.OnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
////            Timber.d("onScrollChange %d %d", scrollY, oldScrollY)
//            HiLog.d(scrollY)
//            var alpha = scrollY
//            if (alpha > 55) {
//               binding.toolbarContainer.visibility = View.VISIBLE
//                binding.boxNav.visibility =View.VISIBLE
//            }
//
//
//        })

    }

    @OptIn(ExperimentalMaterialApi::class, ExperimentalLayoutApi::class)
    override fun initViews() {
        super.initViews()

        // 设置沉浸式状态栏
        QMUIStatusBarHelper.translucent(this)


        binding.goodsList.apply {
            layoutManager = LinearLayoutManager(hostActivity)

        }



        binding.stickCompose.setContent {
            var checkState by remember { mutableStateOf(0) }
            Row(
                modifier = Modifier.fillMaxWidth().padding(start = 15.dp,end = 15.dp),
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically

            ) {
                Image(
                    painter = painterResource(id = R.drawable.free_hot),
                    modifier = Modifier
                        .width(91.dp)
                        .height(30.dp)
                        .clickable {
                              checkState = 0
                        }
                    ,
                    contentDescription = null
                )
              if(checkState == 1){
                  Surface(
                      modifier = Modifier
                          .border(
                              width = 1.dp,
                              color = Color(0xFFFF3333),
                              shape = RoundedCornerShape(50)
                          )
                          .padding(start = 13.dp, top = 4.dp, end = 13.dp, bottom = 4.dp)
                  ) {
                      Text(
                          text = "甄选优品",
                          color = Color(0xFFFF3333),
                          style = TextStyle(
                              fontWeight = FontWeight.W500,
                              fontSize = 14.sp
                          ),
                      )
                  }
              }else{
                  Text(
                      text = "甄选优品",
                      color = Color(0xff888888),
                      style = TextStyle(
                          fontWeight = FontWeight.W500,
                          fontSize = 14.sp
                      ),
                      modifier = Modifier.clickable {
                          checkState = 1
                      }
                  )
              }
              if(checkState == 2)  {
                  Surface(
                      modifier = Modifier
                          .border(
                              width = 1.dp,
                              color = Color(0xFFFF3333),
                              shape = RoundedCornerShape(50)
                          )
                          .padding(start = 13.dp, top = 4.dp, end = 13.dp, bottom = 4.dp)
                  ) {
                      Text(
                          text = "捡漏专区",
                          color = Color(0xFFFF3333),


                      )
                  }
              }else{
                  Text(
                      text = "捡漏专区",
                      color = Color(0xff888888),
                      modifier = Modifier.clickable {
                          checkState = 2
                      }
                  )
              }
               if(checkState == 3){
                   Surface(
                       modifier = Modifier
                           .border(
                               width = 1.dp,
                               color = Color(0xFFFF3333),
                               shape = RoundedCornerShape(50)
                           )
                           .padding(start = 13.dp, top = 4.dp, end = 13.dp, bottom = 4.dp)
                   ) {
                       Text(
                           text = "新人福利",
                           color = Color(0xFFFF3333),
                           modifier = Modifier.clickable {
                               checkState = 3
                           }
                       )
                   }
               }else{
                   Text(
                       text = "新人福利",
                       color = Color(0xff888888),
                       modifier = Modifier.clickable {
                           checkState = 3
                       }
                   )
               }


            }
        }

    }


    companion object {

        private val historyDataList = mutableListOf<HistoryData>(
            HistoryData(R.drawable.photo, "极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo, "极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo, "极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo, "极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo, "极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo, "极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo, "极品校花爱上我", "第五集"),
            HistoryData(R.drawable.photo, "极品校花爱上我", "第五集")

        )
    }
}